package com.game.components
{

	/**
	 * 自动销毁的组件
	 * @author hyy
	 *
	 */
	public class AutoDestroyComponent extends FrameComponent
	{
		protected var duration : int;
		protected var playElapsedTime : int = 0;
		protected var executeFun : Function;

		public function AutoDestroyComponent(type : * = null)
		{
			super(type ? type : AutoDestroyComponent);
		}

		public function setExecuteFun(value : Function) : void
		{
			executeFun = value;
		}

		override public function update(delay : uint) : void
		{
			playElapsedTime += delay;

			if(playElapsedTime >= duration && duration != 0)
			{
				timeOut();
			}
		}

		public function timeOut() : void
		{
			if(executeFun != null)
			{
				executeFun();
			}
			destroy();
		}

		/**
		 *
		 * @param duration
		 *
		 */
		public function setDration(value : int) : void
		{
			if(value <= (duration - playElapsedTime))
				return;

			if(value <= 0)
				destroy();
			playElapsedTime = 0;
			this.duration = value;
		}

		override public function destroy() : void
		{
			super.destroy();
			duration = 0;
			executeFun = null;
			playElapsedTime = 0;
		}
	}
}